<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>新增角色</legend>
</fieldset>

<!-- 由于这是弹出层,引入公共控件 -->
<%
var path = request.ContextPath;
var basePath = request.Scheme +"://"+request.ServerName+":"+request.ServerPort+path+"/";
var staticPath = basePath + "static";
%>
<link rel="import" href="${staticPath}/page/common/common_plug.html">

<div class="center">
    <form class="layui-form" action="">

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">角色名称</label>
                <div class="layui-input-inline">
                    <input type="tel" name="name" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">权限</label>
                <div class="layui-input-inline">
                    <ul id="resource" class="ztree"></ul>
                </div>
            </div>
        </div>


        <div class="layui-form-item layui-form-text col-10">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" name="remark" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit lay-filter="submit_role">提交</button>
                <button type="reset"  class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

</div>


<script>
    $.ajax({
        type: "POST",
        url: "/systemResource/getAllRoleResource" ,
        success: function (res) {
            if (res != null) {
                // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
                var setting = {
                    check: {
                        enable: true,
                        chkStyle: "checkbox",
                        chkboxType: {"Y": "ps", "N": "ps"}
                    }
                };
                // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
                var zTreeObj = $.fn.zTree.init($("#resource"), setting, res.data);
            }
        }
    });

    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit;


        //监听提交
        form.on('submit(submit_role)', function (result) {
            var treeObj = $.fn.zTree.getZTreeObj("resource");
            var nodes = treeObj.getCheckedNodes();
            if (nodes.length > 0) {
                var resourceIds = "";
                for (var i = 0; i < nodes.length; i++) {
                    resourceIds += nodes[i].id + ",";
                }
                result.field.roleIds = resourceIds.substring(0,resourceIds.length-1);
            }

            $.ajax({
                type: "POST",
                url: "/systemRole/addRole",
                async:true,
                data: result.field,
                success: function (data) {
                    if (data != null && data.code == 200) {
                        layer.msg(data.msg);
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.$("#reload").click();
                    } else {
                        layer.msg(data.msg);
                        return false;
                    }
                }
            })
            return false;
        })
    });
</script>
